<?php defined( '_VALID_MOS' ) or die( include("404.php") );

switch($_POST["hidden"])
{

	case "";
	// khoi dau trang khong co gia tri submit. khong lam zi ca
	break;
	
	/* khoi su ly su kien them menu moi */
	case "submit_com_menu_main_menu_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_main_menu($_POST["title"], $core_class->_removesigns($_POST["alias"]), $_POST["menutype"], $_POST["published"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu");
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_main_menu($_POST["title"], $core_class->_removesigns($_POST["alias"]), $_POST["menutype"], $_POST["published"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=main_menu&task=add");
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu");
			exit;
		}

	break;
	
	case "submit_com_menu_main_menu_view";
		if($_POST["task"] == "remove"){
			$myprocess = new process;
			if( $myprocess->process_remove_main_menu_view($_POST["cid"]) <> FALSE ){
				$GLOBALS['msg'] = "";			
			} else { 
				$GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị ! !!! ";
			}
		}
	break;
	
	case "submit_com_menu_item_menu_view";
		if($_POST["task"] == "remove"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_remove_item_menu_view($values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE)
			$GLOBALS['msg'] = "";
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị ! !!! ";
		} else if($_POST["task"] == "unpublish"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_pulish_and_un_publish_item_menu("0", $values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE)
			$GLOBALS['msg'] = "";
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
		}
		else if($_POST["task"] == "publish"){
			$check = FALSE;
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_pulish_and_un_publish_item_menu("1", $values[$row]) <> FALSE)
				$check = TRUE;
			}
			if($check == TRUE)
			$GLOBALS['msg'] = "";
			else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỗi, vui lòng liên hệ quản trị !!! ";
		}
		else if($_POST["task"] == "orderup"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_orderup_item_menu($values[$row]) <> FALSE)
				$GLOBALS['msg'] = "";
				else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỡi, vui lòng liên hệ quản trị !";
			}
		}
		else if($_POST["task"] == "orderdown"){
			$values = $_POST["cid"];
			$myprocess = new process;
			for ($row = 0; $row < count($values); $row++){
				if($myprocess->process_orderdown_item_menu($values[$row]) <> FALSE)					
				$GLOBALS['msg'] = "";
				else $GLOBALS['msg'] = "Hiện tại hệ thống đang gặp lỡi, vui lòng liên hệ quản trị !";
			}
		}
	break;
	
	/* khoi su ly su kien them item menu moi */
	case "submit_com_menu_item_menu_session_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_item_main_session("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], "session", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_item_main_session("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], "session", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=session.add&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien chinh sua item menu session lien ket */
	case "submit_com_menu_item_menu_session_edit";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_edit_item_main_session($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_edit_item_main_session($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=session.edit&id=".$_POST["menuid"]."&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	case "submit_com_menu_item_menu_category_edit";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_edit_item_main_category($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["catid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_edit_item_main_category($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["catid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=category.edit&id=".$_POST["menuid"]."&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	case "submit_com_menu_item_menu_article_edit";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_edit_item_main_article($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["newsid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_edit_item_main_article($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["newsid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=article.edit&id=".$_POST["menuid"]."&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien chinh sua item menu linkout lien ket */
	case "submit_com_menu_item_menu_linkout_edit";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_edit_item_main_linkout($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["browserNav"], $_POST["published"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_edit_item_main_linkout($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["browserNav"], $_POST["published"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=linkout.edit&id=".$_POST["menuid"]."&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien chinh sua item menu contact lien ket */
	case "submit_com_menu_item_menu_contact_edit";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_edit_item_main_contact($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_edit_item_main_contact($_POST["parent"], $_POST["title"], $_POST["link"], $_POST["sectionid"], $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $_POST["menutype"], $_POST["menuid"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=session.edit&id=".$_POST["menuid"]."&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien them item menu moi */
	case "submit_com_menu_item_menu_category_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_item_main_category("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["catid"], "category", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_item_main_category("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["catid"], "category", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=category.add&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien them item menu moi */
	case "submit_com_menu_item_menu_article_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_item_main_article("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["newsid"], "article", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_item_main_article("1", $_POST["parent"], $_POST["title"], $_POST["link"], $_POST["newsid"], "article", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=article.add&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien them item menu moi */
	case "submit_com_menu_item_menu_contact_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_item_main_article("1", $_POST["parent"], $_POST["title"], $_POST["link"], "", "contact", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_item_main_article("1", $_POST["parent"], $_POST["title"], $_POST["link"], "", "contact", $_POST["browserNav"], $_POST["published"], $_POST["publish_title"], $_POST["publish_date"], $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=contact.add&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	/* khoi su ly su kien them item menu lien ket noi dung ben ngoai */
	case "submit_com_menu_item_menu_linkout_add";
		$myprocess = new process;
		if($_POST["task"] == "save"){
			if($myprocess->process_add_item_main_linkout("1", $_POST["parent"], $_POST["title"], $_POST["link"], "", "linkout", $_POST["browserNav"], $_POST["published"], 0, 0, $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if ($_POST["task"] == "apply"){
			if($myprocess->process_add_item_main_linkout("1", $_POST["parent"], $_POST["title"], $_POST["link"], "", "linkout", $_POST["browserNav"], $_POST["published"], 0, 0, $myprocess->process_getmaxid("menu", "order_num"), $_POST["menutype"]) <> FALSE){
				$GLOBALS['msg'] = "Chủ đề đã được thêm thành công!";
				$core_class->_redirect(".?com=com_menu&view=item_menu&task=linkout.add&menutypeid=".$_POST["menutype"]);
				exit();
			} else {
				$GLOBALS['msg'] = "Đã có lỗi thêm chủ đề, vui lòng làm lại!";
			}
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutype"]);
			exit;
		}

	break;
	
	case "submit_com_menu_item_menu_tree";
		if($_POST["task"] == "session"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=session.add&menutypeid=".$_POST["menutypeid"]);exit();
		}else if($_POST["task"] == "category"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=category.add&menutypeid=".$_POST["menutypeid"]);exit();
		}else if($_POST["task"] == "article"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=article.add&menutypeid=".$_POST["menutypeid"]);exit();
		} else if($_POST["task"] == "contact"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=contact.add&menutypeid=".$_POST["menutypeid"]);exit();
		} else if($_POST["task"] == "linkout"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=linkout.add&menutypeid=".$_POST["menutypeid"]);exit();
		} else if($_POST["task"] == "cancel"){
			$core_class->_redirect(".?com=com_menu&view=item_menu&task=view&menutypeid=".$_POST["menutypeid"]);exit();
		}
	break;
		
	default:
		$core_class->_redirect(".");exit();
	break;
}

class process{

	/* ham su ly go bo mot item menu */
	function process_remove_item_menu_view($values){
		include("../protected/dbconnect.php");
		$sql = "Delete from menu where Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	/* ham su ly go bo mot main menu */
	function process_remove_main_menu_view($values){
		include("../protected/dbconnect.php");
		$sql = "Delete from menu_type where Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	/* ham su ly xuat ban va khong xuat ban muc item menu */
	function process_pulish_and_un_publish_item_menu($check, $values){
		include("../protected/dbconnect.php");
		if($check == 0)
		$sql = "Update menu Set `activated` = 0 Where Id = ?";
		else $sql = "Update menu Set `activated` = 1 Where Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("s", $values);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly di chuyen mau tin xuong phia duoi cua item menu
	function process_orderdown_item_menu($pnewsid){

		include("../protected/dbconnect.php");
	
		$sql = "SELECT (SELECT `order_num` from menu WHERE Id = $pnewsid) As `currenOrder`,
				(SELECT `order_num` from menu WHERE `parent_Id` = (SELECT `parent_Id` from menu WHERE Id = $pnewsid)
				AND menu_type_Id = (SELECT menu_type_Id from menu WHERE `Id` = $pnewsid) AND `order_num` > (SELECT `order_num` from menu WHERE Id = $pnewsid) 
				Order by `order_num` LIMIT 1) As `preOrder`, 
				(SELECT Id from menu WHERE `order_num` = (SELECT `order_num` from menu WHERE `parent_Id` = (SELECT `parent_Id` from menu WHERE Id = $pnewsid)
				AND menu_type_Id = (SELECT menu_type_Id from menu WHERE `Id` = $pnewsid) AND `order_num` > (SELECT `order_num` from menu WHERE Id = $pnewsid) 
				Order by `order_num` LIMIT 1)) As `preSesid`";
	
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update menu set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update menu set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $pnewsid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}				 
			else echo $mysqli->error;
		}					
		$cmd->close();
		$mysqli->close();		
	}
	
	// ham su ly di chuyen mau tin xuong phia tren cua item menu
	function process_orderup_item_menu($pnewsid){

		include("../protected/dbconnect.php");
		$sql = "SELECT (SELECT `order_num` from menu WHERE Id = $pnewsid) As `currenOrder`,
				(SELECT max(`order_num`) from menu WHERE `parent_Id` = (SELECT `parent_Id` from menu WHERE Id = $pnewsid)
				AND menu_type_Id = (SELECT menu_type_Id from menu WHERE `Id` = $pnewsid) AND `order_num` < (SELECT `order_num` from menu WHERE Id = $pnewsid) 
				Order by `order_num` LIMIT 1) As `preOrder`, 
				(SELECT Id from menu WHERE `order_num` = (SELECT max(`order_num`) from menu WHERE `parent_Id` = (SELECT `parent_Id` from menu WHERE Id = $pnewsid)
				AND menu_type_Id = (SELECT menu_type_Id from menu WHERE `Id` = $pnewsid) AND `order_num` < (SELECT `order_num` from menu WHERE Id = $pnewsid) 
				Order by `order_num` LIMIT 1)) As `preSesid`";
	
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($currenOrder, $preOrder, $preSesid);
		$cmd->store_result();
		if($cmd->fetch()){
			$sql1 = "update menu set `order_num` = ? where `Id` = ?";
			$cmd = $mysqli->prepare($sql1);
			$cmd->bind_param("ss", $currenOrder, $preSesid);
			if($cmd->execute() <> FALSE){
				$sql2 = "update menu set `order_num` = ? where `Id` = ?";
				$cmd = $mysqli->prepare($sql2);
				$cmd->bind_param("ss", $preOrder, $pnewsid);
				if($cmd->execute() <> FALSE){
					return true;
				}
				else echo $mysqli->error;
				return true;					
			}
			else echo $mysqli->error;
			//echo "Down: " . $currenOrder . "||" . $preOrder . "||" . $preSesid;
		}					
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua lien ket session
	function process_edit_item_main_session( $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE `menu`
				SET `parent_Id` = ?, 
				`title` = ?, 
				`link` = ?, 
				`link_id` = ?, 
				`target` = ?, 
				`activated` = ?, 
				`active_title` = ?, 
				`active_date` = ?, 
				`menu_type_Id` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssss", $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua lien ket category
	function process_edit_item_main_category( $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE `menu`
				SET `parent_Id` = ?, 
				`title` = ?, 
				`link` = ?, 
				`link_id` = ?, 
				`target` = ?, 
				`activated` = ?, 
				`active_title` = ?,
				`active_date` = ?,
				`menu_type_Id` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssss", $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua lien ket article
	function process_edit_item_main_article( $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE `menu`
				SET `parent_Id` = ?, 
				`title` = ?, 
				`link` = ?, 
				`link_id` = ?, 
				`target` = ?, 
				`activated` = ?,
				`active_title` = ?,
				`active_date` = ?,
				`menu_type_Id` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssss", $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua lien ket linkout
	function process_edit_item_main_linkout( $parent_Id, $title, $link, $target, $activated, $menu_type_Id, $menuid ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE `menu`
				SET `parent_Id` = ?, 
				`title` = ?, 
				`link` = ?, 
				`target` = ?, 
				`activated` = ?, 
				`menu_type_Id` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("sssssss", $parent_Id, $title, $link, $target, $activated, $menu_type_Id, $menuid);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su ly chinh sua lien ket contact
	function process_edit_item_main_contact( $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "UPDATE `menu`
				SET `parent_Id` = ?, 
				`title` = ?, 
				`link` = ?, 
				`link_id` = ?, 
				`target` = ?, 
				`activated` = ?, 
				`active_title` = ?, 
				`active_date` = ?, 
				`menu_type_Id` = ?
				WHERE Id = ?";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssss", $parent_Id, $title, $link, $link_id, $target, $activated, $active_title, $active_date, $menu_type_Id, $menuid);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}

	// ham su ly them main menu
	function process_add_main_menu( $title, $alias, $type, $activated ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "Insert into menu_type(`title`, `alias`, `type`, `activated`) VALUES (?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssss", $title, $alias, $type, $activated);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	function process_add_item_main_session( $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into menu (`root`, `parent_Id`, `title`, `link`, `link_id`, `type`, `target`, `activated`, `active_title`, `active_date`, `order_num`, `menu_type_Id`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssssss", $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	function process_add_item_main_article( $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into menu (`root`, `parent_Id`, `title`, `link`, `link_id`, `type`, `target`, `activated`, active_title, active_date, `order_num`, `menu_type_Id`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssssss", $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	function process_add_item_main_category( $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into menu (`root`, `parent_Id`, `title`, `link`, `link_id`, `type`, `target`, `activated`, `active_title`, `active_date`, `order_num`, `menu_type_Id`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssssss", $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	function process_add_item_main_linkout( $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into menu (`root`, `parent_Id`, `title`, `link`, `link_id`, `type`, `target`, `activated`, active_title, active_date, `order_num`, `menu_type_Id`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssssss", $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	function process_add_item_main_contact( $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id ){
		$myprocess = new process;
		include("../protected/dbconnect.php");
		$sql = "INSERT into menu (`root`, `parent_Id`, `title`, `link`, `link_id`, `type`, `target`, `activated`, `active_title`, `active_date`, `order_num`, `menu_type_Id`)
				VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		$cmd = $mysqli->prepare($sql);
		$cmd->bind_param("ssssssssssss", $root, $parent_Id, $title, $link, $link_id, $type, $target, $activated, $active_title, $active_date, $order_num, $menu_type_Id);
		if($cmd->execute() <> FALSE) return true;
		else echo $mysqli->error;
		$cmd->close();
		$mysqli->close();
	}
	
	// ham su lay so thu tu lon nhat cho moi mau tin
	function process_getmaxid($table, $column){
		include("../protected/dbconnect.php");
		$sql = "select MAX(`$column`)+1 As `MaxId` from `$table`;";
		$cmd = $mysqli->prepare($sql);
		$cmd->execute();
		$cmd->bind_result($MaxId);
		if($cmd->fetch()){
			if($MaxId == 0)	return 1;
			else return $MaxId;
		}
		$cmd->close();
		$mysqli->close();
	}
}